package com.module.user.ui.completePersonalInfo

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.common.app.data.bean.BaseCityModel
import com.common.app.data.bean.TimeSelectorBean
import com.common.app.data.bean.TimeSelectorModel
import com.common.app.utls.TimeUtils
import com.common.app.utls.TimeUtils.getCurMonth
import com.common.app.utls.TimeUtils.getCurYear
import com.common.app.utls.TimeUtils.getDay
import com.common.app.utls.TimeUtils.getDaysInMonth
import com.common.app.utls.TimeUtils.getMonth
import com.common.app.utls.TimeUtils.getTimeDateLong
import com.common.app.utls.TimeUtils.getYearListByTimestampAndOffset
import com.common.app.utls.TimeUtils.getYears
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.yes
import com.common.base.utils.AppUtils
import com.common.base.utils.JsonUtils
import com.common.base.utils.ViewUtils
import com.contrarywind.listener.OnItemSelectedListener
import com.module.user.R
import com.module.user.databinding.UserDialogDateBirthSelectBinding
import com.module.user.databinding.UserDialogGenderSelectBinding


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：14/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class GenderSelectDialog(context: Context) : Dialog(context, R.style.custom_dialog_sv),
    View.OnClickListener {
    private var binding: UserDialogGenderSelectBinding? = null

    /**
     *
     */
    private val mContext: Context

    /**
     * 点击监听
     */
    private var listener: OnSelectListener? = null


    // 性别适配器数据
    private var genderAdapter: ArrayWheelAdapter<*>? = null
    private var genderWheelListener: OnItemSelectedListener? = null
    private var genderItems = mutableListOf<BaseCityModel>()
    private var genderItem: BaseCityModel? = null

    /**
     * +
     * 构造方法
     *
     * @param context
     */
    init {
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = UserDialogGenderSelectBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }

        initData()
        initStartOptionPicker()
        binding?.dialogRoot?.setOnClickListener(onClick = this)
        binding?.negativeButton?.setOnClickListener(onClick = this)
    }

    private fun initData() {
        genderItems.add(BaseCityModel(name = "男",province = null,pinyin = null,pCode = null,code = "1"))
        genderItems.add(BaseCityModel(name = "女",province = null,pinyin = null,pCode = null,code = "0"))
//        genderItems.add(BaseCityModel(name = "秘密",province = null,pinyin = null,pCode = null,code = "-1"))
    }

    //开始条件选择器初始化
    private fun initStartOptionPicker() {

        binding?.let {

            //字体样式，默认是等宽字体
            it.wvGender.setTypeface(Typeface.SANS_SERIF)

            //设置滚轮文字大小
            it.wvGender.setTextSize(17F)

            //设置分割线的颜色
            it.wvGender.setDividerColor(Color.TRANSPARENT)

            // 设置分割线之间的文字的颜色
            it.wvGender.setTextColorCenter(context.resources.getColor(com.common.app.R.color.text_content))

            // 设置分割线以外文字的颜色
            it.wvGender.setTextColorOut(Color.LTGRAY)

            // 条目间距倍数
            it.wvGender.setLineSpacingMultiplier(2.6F)

            // 绑定适配器
            genderAdapter = ArrayWheelAdapter(genderItems)
            it.wvGender.adapter = genderAdapter // 设置显示数据
            it.wvGender.currentItem = 0 // 初始化时显示的数据
            genderItem = genderItems[0]

            it.wvGender.setIsOptions(true)

            it.wvGender.setCyclic(false)

            it.wvGender.setItemsVisibleCount(4)

            // 联动监听器
            genderWheelListener = OnItemSelectedListener { index ->
                if (genderItems.size > 0 && !TextUtils.isEmpty(genderItems[index].code) && genderItems.size > index) {
                    genderItem = genderItems[index]
                }
            }
            // 添加联动监听
            it.wvGender.setOnItemSelectedListener(genderWheelListener)
        }
    }

    override fun onClick(view: View) {
        when (view) {
            binding?.negativeButton -> {
                genderItem?.let { listener?.onSelectClick(it) }
            }
            binding?.dialogRoot -> {
            }
        }
        dismiss()
    }

    override fun show() {
        super.show()
        window?.apply {
            //设置宽度
            attributes.width = ViewGroup.LayoutParams.MATCH_PARENT
            attributes.gravity = Gravity.BOTTOM //设置dialog 在布局中的位置
            setWindowAnimations(com.common.app.R.style.bottom_style_dialog) //添加动画:
            setBackgroundDrawable(ColorDrawable(mContext.getColor(com.common.app.R.color.transparent))) // android:windowBackground
        }
    }

    override fun dismiss() {
        super.dismiss()
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnSelectListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnSelectListener {
        fun onSelectClick(mBean: BaseCityModel)
    }
}